package cn.com.startai.huadiantrainer.app.activity;

import android.Manifest;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.widget.Toast;

import java.io.File;
import java.util.ArrayList;
import java.util.concurrent.TimeUnit;

import cn.com.startai.common.utils.CThreadPoolUtils;
import cn.com.startai.common.utils.CUtils;
import cn.com.startai.common.utils.permission.CPermissionHelper;
import cn.com.startai.huadiantrainer.global.Debuger;
import cn.com.startai.huadiantrainer.global.FileManager;
import cn.com.startai.huadiantrainer.global.speak.SpeakManager;
import cn.com.startai.huadiantrainer.js.IJsRequestAnd;
import cn.com.startai.huadiantrainer.mutual.JsActivity;
import cn.com.swain.baselib.app.IApp.IService;
import cn.com.swain.baselib.log.Tlog;
import cn.com.swain.baselib.permission.PermissionGroup;
import cn.com.swain.baselib.permission.PermissionHelper;
import cn.com.swain.baselib.permission.PermissionRequest;

/**
 * author Guoqiang_Sun
 * date 2019/5/10
 * desc
 */
public class HomeActivity extends WebActivity implements IJsRequestAnd {

    private PermissionRequest mPermissionRequest;
    private IService mService;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestPermission();
        if (mService == null) {
            final JsActivity jsActivity = new JsActivity(getApplication(), this);
            mService = jsActivity;
            mService.onSCreate();
            regIJsInterfaceInstance(jsActivity);
            loadWEB();
        }
        Debuger.getInstance().onSCreate();
    }

    @Override
    protected void onResume() {
        if (mService != null) {
            mService.onSResume();
        }
        Debuger.getInstance().onSResume();
        super.onResume();
    }

    @Override
    protected void onPause() {
        if (mService != null) {
            mService.onSPause();
        }
        Debuger.getInstance().onSPause();
        super.onPause();
    }

    @Override
    protected void onDestroy() {
        if (mPermissionRequest != null) {
            mPermissionRequest.release();
            mPermissionRequest = null;
        }
        if (mService != null) {
            mService.onSDestroy();
            mService = null;
        }

        Debuger.getInstance().onSDestroy();

        SpeakManager.getInstance().release();

        super.onDestroy();
    }

    private void requestPermission() {



        Context applicationContext = getApplicationContext();

        ArrayList<String> permissions = new ArrayList<>(2);

        if (!PermissionHelper.isGranted(applicationContext, Manifest.permission.WRITE_EXTERNAL_STORAGE)
                || !PermissionHelper.isGranted(applicationContext, Manifest.permission.READ_EXTERNAL_STORAGE)) {

            permissions.add(PermissionGroup.STORAGE);

        } else {

            FileManager.getInstance().recreate(getApplication());
            Debuger.getInstance().reCheckLogRecord(applicationContext);

            SpeakManager.getInstance().init(getApplication());

        }

        if (!PermissionHelper.isGranted(applicationContext, Manifest.permission.ACCESS_COARSE_LOCATION)
                || !PermissionHelper.isGranted(applicationContext, Manifest.permission.ACCESS_FINE_LOCATION)) {
            permissions.add(PermissionGroup.LOCATION);
        }


        if (permissions.size() > 0) {

            String[] strings = permissions.toArray(new String[0]);
            if (mPermissionRequest == null) {
                mPermissionRequest = new PermissionRequest(this);
            }
            mPermissionRequest.requestPermissions(new PermissionRequest.OnPermissionFinish() {
                @Override
                public void onAllPermissionRequestFinish() {
                    Tlog.v(TAG, "HomeActivity onAllPermissionRequestFinish() ");


                }
            }, new PermissionRequest.OnPermissionResult() {
                @Override
                public boolean onPermissionRequestResult(String permission, boolean granted) {

                    Tlog.v(TAG, "HomeActivity permission " + permission + " granted:" + granted);

                    if (granted && PermissionGroup.STORAGE.equalsIgnoreCase(permission)) {
                        FileManager.getInstance().recreate(getApplication());
                        Debuger.getInstance().reCheckLogRecord(getApplication());
                        SpeakManager.getInstance().init(getApplication());

                    }

                    return true;
                }
            }, strings);
        } else {
            Tlog.d(TAG, "HomeActivity no permission need request ");
        }

    }


    @Override
    public String getLoadUrl() {

        if (Debuger.isH5Debug && PermissionHelper.isGranted(getApplication(),
                Manifest.permission.READ_EXTERNAL_STORAGE)) {
            File localH5Resource = Debuger.getInstance().getLocalH5Resource();
            if (localH5Resource != null && localH5Resource.exists()) {
                Toast.makeText(getApplicationContext(), "load from SDCard", Toast.LENGTH_LONG).show();
                return "file://" + localH5Resource.getAbsolutePath();
            }
        }

        return super.getLoadUrl();
    }


    @Override
    public void onRequestPermissionsResult(int requestCode,
                                           @NonNull String[] permissions,
                                           @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);

        if (mPermissionRequest != null) {
            mPermissionRequest.onRequestPermissionsResult(requestCode, permissions, grantResults);
        }

    }


    @Override
    public void jFinish() {

        if (mService != null) {
            mService.onSFinish();
        }
        Debuger.getInstance().onSFinish();

        finish();
    }
}
